package com.ye.controller;

import com.ye.domain.Comments;
import com.ye.domain.R;
import com.ye.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RefreshScope
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/getCommentsByMusicId")
    public R secondLevelMerge(Long musicId){
        List<Comments> comments = commentService.secondLevelMerge(musicId);
        return R.ok(comments);
    }

    //    添加一条评论
    @GetMapping("/insertFirstComment")
    public R firstComment(@RequestParam("musicId") Long musicId, @RequestParam("content") String content, @RequestParam("userId") Long userId){
        try {
            commentService.firstComment(musicId, content, userId);
            return R.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error(500,"服务器故障，请联系管理员！");
    }

    //    添加一条回复
    @GetMapping("/reply")
    public R reply(@RequestParam("musicId") Long musicId,@RequestParam("parentId") Long parentId,@RequestParam("content") String content,@RequestParam("userId") Long userId){
        try {
            commentService.reply(musicId, parentId, content, userId);
            return R.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error(500,"服务器故障，请联系管理员！");
    }



}

